/**
 * @file models/closedPositions.js
 * @author
 */

import { routerRedux } from 'dva/router';
import api from '../api';
import { delay } from '../utils/sagaEffects';

export default {
  namespace: 'retailClosedPositions',
  state: {
    list: [],
    page: {},
    selected: undefined,
    isDetailOpen: false,
    closeOrder: undefined,
  },
  reducers: {
    getListSuccess(state, action) {
      const { payload: { response } } = action;
      return {
        ...state,
        list: response.data,
        page: response.page,
        selected: undefined,
        isDetailOpen: false,
      };
    },
    changeSelected(state, action) {
      const { payload: { selected } } = action;
      return {
        ...state,
        isDetailOpen: (selected !== undefined),
        selected,
      };
    },
    changeDetailOpen(state, action) {
      const { payload: { open } } = action;
      return {
        ...state,
        isDetailOpen: open,
      };
    },
    getCloseOrderSuccess(state, action) {
      const { payload: { response } } = action;
      return {
        ...state,
        closeOrder: response.data,
      };
    },
  },
  effects: {
    * getClosedPositions({ payload: query }, { call, put }) {
      const response = yield call(api.getRetailClosedPositions, query);
      yield put({
        type: 'getListSuccess',
        payload: { response },
      });
    },
    * getCloseOrder({ payload: query }, { call, put }) {
      const response = yield call(api.getRetailCloseOrder, query);
      yield put({
        type: 'getCloseOrderSuccess',
        payload: { response },
      });
    },
  },
  subscriptions: {},
};
